package com.clouddot.baipai.ui.fragment.H;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.clouddot.baipai.R;
import com.clouddot.baipai.component.DaggerIAttendFragmentComponent;
import com.clouddot.baipai.contract.IAttendFragmentContract;
import com.clouddot.baipai.http.entity.response.getClassRecordResponse;
import com.clouddot.baipai.http.entity.response.getRecordByIdResponse;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.module.AttendFragmentModule;
import com.clouddot.baipai.ui.activity.H_UMainActivity;
import com.clouddot.baipai.ui.adapter.H_ClassRecordAdapter;
import com.clouddot.baipai.ui.adapter.StudentRecordAdapter;
import com.clouddot.baipai.util.Toast.ToastUtil;
import com.clouddot.baipai.util.viewUtil.ViewAnim;

import org.wxy.sdk.base.IBasePresenter;
import org.wxy.sdk.base.impl.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by wxy on 2018/8/23.
 * 横版班牌考勤的主页面
 */

public class H_AttendFrgment extends BaseFragment implements IAttendFragmentContract.IView {
    private int currentPage = 1;
    private H_ClassRecordAdapter getAttendRecordAdapter;
    private StudentRecordAdapter getRecordByIdAdapter;
    private List<getClassRecordResponse> mDataList = new ArrayList<>();
    private List<getRecordByIdResponse.attentionRecord> attentionRecords = new ArrayList<>();

    @Inject
    IAttendFragmentContract.IPresenter mPresenter;
    @BindView(R.id.RecyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.leftLoading)
    ImageView leftLoading;
    @BindView(R.id.loadingLP)
    RelativeLayout loadingLP;
    @BindView(R.id.attend)
    TextView attend;
    @BindView(R.id.scanCardNum)
    TextView scanCardNum;

    @Override
    public IBasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    protected View inflateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
        return inflater.inflate(R.layout.h_fragment_attend, null);
    }

    @Override
    protected void initViews() {
        DaggerIAttendFragmentComponent.builder().attendFragmentModule(new AttendFragmentModule(this)).build().inject(this);
        if (!Env.getAppContext().StudentID.isEmpty()) {
            attend.setText(getActivity().getResources().getString(R.string.personal_attend));
            mPresenter.getStudentAttendRecord(1, 60);
            scanCardNum.setVisibility(View.GONE);
        } else {
            attend.setText(getActivity().getResources().getString(R.string.class_attend));
            mPresenter.getClassAttendRecord(Env.getAppContext().ClassID);
            scanCardNum.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initVariables() {
        Animation loadingAnim = ViewAnim.getRotateImg(getActivity());
        leftLoading.startAnimation(loadingAnim);
        recyclerView.addOnScrollListener(H_UMainActivity.listener);
    }
    /**
     * fragment 隐藏和显现触发的方法，第一次生成的时候不会触发。
     *  hidden 为 false --> 显现 ；为 true --> 隐藏
     * @param hidden
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            loadingLP.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
            if (!Env.getAppContext().StudentID.isEmpty()) {
                attend.setText(getActivity().getResources().getString(R.string.personal_attend));
                attentionRecords.clear();
                currentPage = 1;
                mPresenter.getStudentAttendRecord(currentPage, 60);
                scanCardNum.setVisibility(View.GONE);
            } else {
                attend.setText(getActivity().getResources().getString(R.string.class_attend));
                mPresenter.getClassAttendRecord(Env.getAppContext().ClassID);
                scanCardNum.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    protected void loadData() {
    }


    /**
     *
     * @param flag 1 --> 获取班级考勤记录
     *             2 --> 获取学生考勤记录
     * @param object
     */
    @Override
    public void success(int flag, Object object) {
        if (flag == 1) {
            refluesClassAttend(object);
        } else if (flag == 2) {
            refluesStudentAttend(object);
        }
        loadingLP.setVisibility(View.GONE);
        recyclerView.setVisibility(View.VISIBLE);
    }

    @Override
    public void fail(int flag, String errMsg) {
        ToastUtil.showToast(getActivity(), errMsg);
    }

    /**
     * 更新班级考勤数据
     * @param object
     */
    private void refluesClassAttend(Object object) {
        ArrayList<getClassRecordResponse> response = (ArrayList<getClassRecordResponse>) object;
        if (response.isEmpty()) {
            return;
        }
        mDataList = response;
        int i = 0;
        for (getClassRecordResponse classAttendRecord : mDataList) {
            if (classAttendRecord.cardtimes > 0) i++;
        }
        scanCardNum.setText("全部人数： " + mDataList.size() + " 人\n" + "已经签到： " + i + " 人");
        getAttendRecordAdapter = new H_ClassRecordAdapter(mDataList);
        GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 7, OrientationHelper.VERTICAL, false);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(getAttendRecordAdapter);
    }

    /**
     * 更新学生考勤数据
     * @param object
     */
    private void refluesStudentAttend(Object object) {
        getRecordByIdResponse response = (getRecordByIdResponse) object;
        if (response.EntityList.isEmpty()) {
            if (getRecordByIdAdapter != null) getRecordByIdAdapter.loadMoreEnd();
            return;
        }
        if (attentionRecords.isEmpty()) {
            attentionRecords = response.EntityList;
            GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 6, OrientationHelper.VERTICAL, false);
            mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(mLayoutManager);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            getRecordByIdAdapter = new StudentRecordAdapter(attentionRecords);
            getRecordByIdAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
                    mPresenter.getStudentAttendRecord(++currentPage, 60);
                }
            });
            recyclerView.setAdapter(getRecordByIdAdapter);
        } else {
            getRecordByIdAdapter.addData(response.EntityList);
            getRecordByIdAdapter.loadMoreComplete();
        }
    }


}
